#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyAnimation>
MypushButton::MypushButton(QString normalImag, QString pressImg)
{
    this->normalImgPath=normalImag;
    this->pressedImgPath=pressImg;

    QPixmap pix;
    bool ret = pix.load((normalImag));
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;
    }

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void MypushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->pressedImgPath !="")//传入图片不为空，切图
    {

        QPixmap pix;
        bool ret = pix.load(this->pressedImgPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类执行其他的内容
    return QPushButton::mousePressEvent(e);
}

void MypushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pressedImgPath !="")//传入图片不为空，切图
    {

        QPixmap pix;
        bool ret = pix.load(this->normalImgPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类执行其他的内容
    return QPushButton::mouseReleaseEvent(e);
}

void MypushButton::zoom1()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()-50,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

void MypushButton::zoom2()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y()-50,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}
